package com.hopu.dy.service.impl;


import com.hopu.dy.mapper.UserMapper;
import com.hopu.dy.pojo.User;
import com.hopu.dy.service.IUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletContext;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service  //标签
public class UserServiceImpl implements IUserService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private ServletContext servletContext;  //引用全局变量

    @Override
    public void registerOrLogin(User user) {
        User user1 = userMapper.selectByEmail(user.getEmail());
        if (user1 == null) {
            user.setRegisterTime(LocalDateTime.now());
            userMapper.insert(user);
        }
        //获取原来发送的验证码
        String captcha = servletContext.getAttribute(user.getEmail()).toString();
        if (!captcha.equals(user.getPassword())) {
            throw new RuntimeException("验证码错误");
        }
    }


    @Override
    public User getUserByEmail(String email) {
        return userMapper.selectByEmail(email);
    }

    @Override
    public void updateById(User user){
        userMapper.update(user);
    }
}
